package com.struts2.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.struts2.student.hibernate.SessionFactory;
import com.struts2.util.Page;


public class DaoImpl implements Dao {
	/**
	 * 根据主键获取对像并尝试将对像转为指定类型
	 * @param clazz
	 * @param id
	 * @return
	 */
	public <T> T get(Class<T> clazz, Serializable id) {
		Session session = SessionFactory.getSession();
		T t = session.get(clazz, id);
		session.close();
		return t;
	}
	/**
	 * 新增
	 * @param obj
	 */
	public void save(Object obj) {
		Session session = SessionFactory.getSession();
		Transaction t = session.beginTransaction();
		session.save(obj);
		t.commit();
		session.close();
	}
	/**
	 * 修改
	 * @param obj
	 */
	public void update(Object obj) {
		Session session = SessionFactory.getSession();
		Transaction t = session.beginTransaction();
		session.update(obj);
		t.commit();
		session.close();
	}
	/**
	 * 新增或者修改
	 * @param obj
	 */
	public void saveOrUpdate(Object obj) {
		Session session = SessionFactory.getSession();
		Transaction t = session.beginTransaction();
		session.saveOrUpdate(obj);
		t.commit();
		session.close();
	}
	/**
	 * 删除
	 * @param obj
	 */
	public void delete(Object obj) {
		Session session = SessionFactory.getSession();
		Transaction t = session.beginTransaction();
		session.delete(obj);
		t.commit();
		session.close();
	}
	/**
	 * 查询
	 * @param queryString
	 * @param clazz
	 * @return
	 */
	public <T> T find(String queryString, Class<T> clazz) {
		T t = null;
		
		Session session = SessionFactory.getSession();
		Query query = session.createQuery(queryString);
		List list = query.list();
		if(list!=null && list.size()>0){
			t = (T) list.get(0);
		}
		session.close();
		
		return t;
	}
	/**
	 * 分页查询
	 * @param page
	 * @param queryString
	 */
	public void searchHql(Page page, String queryString) {
		Session session = SessionFactory.getSession();
		Query query = session.createQuery(queryString);
		query.setFirstResult((page.getPageNo()-1)*page.getPageSize());
		query.setMaxResults(page.getPageSize());
		List list = query.list();
		page.setList(list);
		
		try {
			if(queryString.indexOf("select") >= 0){
				int index = queryString.indexOf("from ");
				if(index < 0){
					index = queryString.indexOf("FROM ");
				}
				queryString = queryString.substring(index);
			}
			query = session.createQuery("select count(1) " + queryString);
			list = query.list();
			page.setTotalNum(Integer.parseInt(list.get(0).toString()));
		} catch (Exception e) {
			try {
				throw new Exception("请检查from关键字是否为不规则（不为全部小写或大写）");
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
		session.close();
	}
	/**
	 * 查询
	 * @param queryString
	 * @return
	 */
	public List queryHql(String queryString) {
		Session session = SessionFactory.getSession();
		Query query = session.createQuery(queryString);
		List list = query.list();
		session.close();
		return list;
	}
	/**
	 * 分页查询
	 * @param page
	 * @param queryString
	 */
	public void searchSql(Page page, String queryString) {
		Session session = SessionFactory.getSession();
		Query query = session.createSQLQuery(queryString + " limit "+((page.getPageNo()-1)*page.getPageSize())+","+page.getPageSize());
		List list = query.list();
		page.setList(list);
		
		int index = queryString.indexOf("from ");
		if(index < 0){
			index = queryString.indexOf("FROM ");
		}
		queryString = queryString.substring(index);
		query = session.createSQLQuery("select count(1) " + queryString);
		page.setTotalNum(Integer.valueOf(query.list().get(0).toString()));
		
		session.close();
	}
	/**
	 * 查询
	 * @param queryString
	 * @return
	 */
	public List querySql(String queryString) {
		Session session = SessionFactory.getSession();
		Query query = session.createSQLQuery(queryString);
		List list = query.list();
		session.close();
		return list;
	}
	/**
	 * 执行更新、删除
	 * @param hql
	 * @return
	 */
	public int executeUpdate(String hql) {
		Session session = SessionFactory.getSession();
		Query query = session.createQuery(hql);
		int result = query.executeUpdate();
		session.close();
		return result;
	}
	/**
	 * 执行更新、删除
	 * @param hql
	 * @return
	 */
	public int executeUpdate(String hql, Map<String, Object> param) {
		Session session = SessionFactory.getSession();
		Query query = session.createQuery(hql);
		if(param!=null && param.size()>0){
			Set<String> keySet = param.keySet();
			for(String key : keySet){
				query.setParameter(key, param.get(key));
			}
		}
		int result = query.executeUpdate();
		session.close();
		return result;
	}
	
}
